Package

Source Code of DataSource

import java.io.Serializable;
import java.util.ArrayList;
import java.util.GregorianCalendar;


public class DataSource implements Serializable {
 
  private static final long serialVersionUID = 8554262013060381750L;
  private ArrayList <Measuring> measurings = new ArrayList<Measuring>();
 
  public String add(Measuring measuring, String password)
  throws ApplicationException{
    String retArg = "";
    //1. Passwort testen
    if(measuring.getPoint().equalsPassword(password)){
      if(this.isValid(measuring)==null){
       
        if(this.measurings.isEmpty()){
          this.measurings.add(measuring);
         
        } else{
          Measuring lastPointMeasuring = getlastPointMeasuring(measuring.getPoint());
          if(lastPointMeasuring == null){
            this.measurings.add(measuring);
          }else if(measuring.getTime().after(lastPointMeasuring.getTime())){
            this.measurings.add(measuring);
            //ueberpruefe die aktuelle Liste
            //if(isTwoMeasureValuesOverBorder(measuring.getPoint().getUnitSystem())){
            if(isTwoMeasureValuesOverBorder(measuring,lastPointMeasuring)){
              retArg += "Last two Values over Border";
              throw new ApplicationException(retArg);
            }
            if(isHalfOverBorder(measuring)){
              retArg += "Average of the Values in the last hour over Border";
              throw new ApplicationException(retArg);
            }
          }else throw new ApplicationException("This time is earlier or equal than the last time");
        }
        if(retArg.equals("")){
          return null;
        }else return retArg.substring(1);
      }else throw new ApplicationException(isValid(measuring));
    } else throw new ApplicationException("Wrong Password");
  }
 
  /**
   *
   * @param i index des messeintrags der entfernt werden soll
   * @param pwd passwort der jeweiligen messstation
   * @return null wenn entfernt werden konnte sonst "wrong password"
   */
  public String remove(int i, String pwd)throws ApplicationException {
      if(this.measurings.get(i).getPoint().equalsPassword(pwd)){
        this.measurings.remove(i);
        return null;
      }else throw new ApplicationException("Wrong Password")
  }
 
  /**
   *
   * @return Groesse der MeasuringsArraylist
   */
  public Integer countMeasurings(){
    return this.measurings.size();
  }
 
  private Measuring getlastPointMeasuring(Point p){
    for(int i=countMeasurings()-1;i>=0;i--){
      if(measurings.get(i).getPoint().getId()==p.getId()){
        return measurings.get(i);
      }
    }
    return null;
  }

  /**
   *
   * @return true wenn die letzten beiden messwerte ueber dem grenzwert waren, ansonsten false
   */
  private boolean isTwoMeasureValuesOverBorder(Measuring measuring, Measuring lastPointMeasuring)throws ApplicationException{
    UnitSystem us = measuring.getPoint().getUnitSystem();
    if(countMeasurings() >= 2){
      if(us.transform(measuring.getValue(), us.getBaseUnit()).getValue() >
      us.getBorder().getValue() && us.transform(lastPointMeasuring.getValue(), us.getBaseUnit()).getValue()>
      us.getBorder().getValue()){
        return true;
      }
    }
    return false;
  }
 
  /**
   *
   * @return die aktuelle id in der messwertliste. also im normalfall die letzte und somit die groesse der arraylist -1
   */
  public Integer getId(){
    return this.measurings.size()-1;
  }
 
  /**
   *
   * @param m measutingobjekt
   * @return null wenn messwert korrekt, ansonsten eine der Fehlermeldungen
   * @throws ApplicationException
   */
  private String isValid(Measuring m) throws ApplicationException {
    String retArg = "";
   
    //1. ueberpruefung auf MIN, MAX Werte bei value
    UnitSystem us = m.getPoint().getUnitSystem();
   
    if(us.transform(m.getValue(), us.getBaseUnit()).getValue() < m.getPoint().getUnitSystem().getMin().getValue() ||
       us.transform(m.getValue(), us.getBaseUnit()).getValue() > m.getPoint().getUnitSystem().getMax().getValue())
    {
      return retArg+="Value is not plausible";
    }

    //2. ueberpruefung auf MIN, Max Werte bei Datum
    if( m.getTime().after(m.getPoint().getMAXTIME()) ||
        m.getTime().before(m.getPoint().getMINTIME()) )
    {
      return retArg+="Time is not plausible";
    }
   
    //3. Pruefen ob Value mehr als doppelt so gross wie der Grenzwert ist
    if (retArg.equals("")) {
      if ((m.getPoint().getUnitSystem().getBorder().getValue())*2 < m.getValue().getValue()) {
        return "Value twice over border";
      }
      else {
        return null;
      }
    }
    else {
      return retArg.substring(1);
    }   
  }
 
  /**
   *
   * @return true wenn in der letzten stunde mindestens 5 messwerte erfasst wurden und mehr als die haelfte von diesen ueber dem grenzwert liegen
   */
  private boolean isHalfOverBorder(Measuring measuring)throws ApplicationException
    int counter = 0;
      int bcounter = 0;
      UnitSystem unitsystem = measuring.getPoint().getUnitSystem();
      GregorianCalendar lowerBorder = (GregorianCalendar)this.measurings.get(this.measurings.size()-1).getTime().clone();
      lowerBorder.roll(GregorianCalendar.HOUR,-1);
 
      if (this.measurings.size() > 0) {
        for (int i = this.measurings.size()-1; i >= 0; i--) {
          if ((lowerBorder.before(this.measurings.get(i).getTime()) ||
            lowerBorder.equals(this.measurings.get(i).getTime()))&&
            (this.measurings.get(i).getPoint().getId()==measuring.getPoint().getId())) {
           
            counter++;
           
            if(((unitsystem.transform(this.measurings.get(i).getValue(), unitsystem.getBaseUnit()).getValue() > unitsystem.getBorder().getValue()))){
               
              bcounter++;
            }
          }else {
            break;
          }
        }
      }else {
        return false;
      }
     
      if (bcounter != 0) {
        if ((double)bcounter/(double)counter > 0.5 && counter >= 5) {
          return true;
        }else {
          return false;
        }
      }else {
        return false;
      }
  }
 
  public Measuring getMeasuring(int i) {
    return this.measurings.get(i);
  }
}
TOP

Related Classes of DataSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.